package net.qiqb.core.domain.definition;

import net.qiqb.core.build.AggregateRootDefineBuilder;
import net.qiqb.core.domain.annotation.AggregatePersistence;
import net.qiqb.core.domain.annotation.AggregateRootLoader;
import net.qiqb.core.domain.annotation.AttachedAggregatePersistence;
import net.qiqb.core.domain.annotation.ExtendedAggregatePersistence;

import java.util.Map;

/**
 * 聚合根定义
 */
public class AggregateRootDefine extends EntityDefine {

    private final Map<String, AggregateRootLoader<?, ?>> loaderMap;

    private final AggregatePersistence<?> aggregatePersistence;

    private final Map<String, AttachedAggregatePersistence<?, ?>> attachedAggregatePersistenceMap;

    private ExtendedAggregatePersistence<?> extendedAggregatePersistence;

    public AggregateRootDefine(AggregateRootDefineBuilder builder) {
        super(builder);
        this.loaderMap = builder.getBusinessIdsLoader();
        this.aggregatePersistence = builder.getAggregatePersistence();
        this.attachedAggregatePersistenceMap = builder.getAttachedAggregatePersistenceMap();
        this.extendedAggregatePersistence = builder.getExtendedAggregatePersistence();
    }


    public <BID, AR> AggregateRootLoader<BID, AR> getAggregateRootLoader(String bidName) {
        return (AggregateRootLoader<BID, AR>) loaderMap.get(bidName);
    }

    public <AR> AggregatePersistence<AR> getAggregatePersistence() {
        return (AggregatePersistence<AR>) aggregatePersistence;
    }

    public <AR, D> AttachedAggregatePersistence<AR, D> getAttachedAggregatePersistence(Class<?> aClass) {
        return (AttachedAggregatePersistence<AR, D>) attachedAggregatePersistenceMap.get(aClass.getName());
    }

    public ExtendedAggregatePersistence getExtendedAggregatePersistence() {
        return extendedAggregatePersistence;
    }
}
